package com.androidhuman.example.mapsv2example;


import java.util.ArrayList;





import com.androidhuman.example.mapsv2example.ListActivity.DataAdapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View.OnClickListener;

public class LetterActivity extends FragmentActivity {

	
	SharedPreferences userPrefs;
	SharedPreferences.Editor userPrefsEditor;


	
	TextView text;
	TextView user;

	static Context mContext;

	private ListView listview;
	private MessDataAdapter adapter;

	private ArrayList<MessData> alist;
	private Context context;



	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		userPrefs = getSharedPreferences("USER", MODE_PRIVATE);
		userPrefsEditor = userPrefs.edit(); 
		//nickname=userPrefs.getString("nickname", getString(R.string.nickname)); 

		setContentView(R.layout.activity_letter);
		
		
	
		



		View btn_close = (View) findViewById(R.id.bt_close);
		btn_close.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				finish();
			}
		});


		mContext = getApplicationContext();


		
		
		listview = (ListView) findViewById(R.id.list_letter);
		alist = new ArrayList<MessData>();
		adapter = new MessDataAdapter(this, alist);
		listview.setAdapter(adapter);

		context = this;

	

		adapter.insert(new MessData(this, 3,"쪽지 샘플", "김제보"), 0);

		
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View arg1, int position,
					long arg3) {
				
				//Toast.makeText(context, Integer.toString(position) ,Toast.LENGTH_SHORT).show();
				
			}
		});
		

	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		return true;
	}

	
	

	public void call()
	{
		String temp = userPrefs.getString("nickname", "올"); 
		Toast.makeText(this, "' "+temp+" '"+"(으)로 변경 되었습니다.", Toast.LENGTH_SHORT).show();
		
		userPrefs = getSharedPreferences("USER", MODE_PRIVATE);
		userPrefsEditor = userPrefs.edit(); 
		//ParentActivity.nickName=userPrefs.getString("nickname", getString(R.string.nickname));
		//((ParentActivity) ParentActivity.top).loadNickName();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
//		if (id == R.id.action_settings) {
//			return true;
//		}
		return super.onOptionsItemSelected(item);
	}

	
	
	class MessDataAdapter extends ArrayAdapter<MessData> {
		private LayoutInflater mInflater;

		public MessDataAdapter(Context context, ArrayList<MessData> object) {
			super(context, 0, object);

			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		}

		public View getView(int position, View v, ViewGroup parent) {
			View view = null;
			if (v == null) {
				view = mInflater.inflate(R.layout.letter_custom, null);
			} else {
				view = v;
			}
			final MessData data = this.getItem(position);
			if (data != null) {
				
				text = (TextView) view.findViewById(R.id.mylog_text);
				user = (TextView) view.findViewById(R.id.mylog_user);
				
				text.setText(data.getText());
				user.setText(data.getUser());
				
				
			}
			return view;
		}
	}

	private class MessData {

		private int Type;
		private String Text;
		private String User;

		public MessData(Context context, int mType, String mText, String mUser) {
			
			Type = mType;
			Text = mText;
			User = mUser;


		}


	
		public int getType(){
			return Type;
		}

		public String getText() {
			return Text;
		}

		public String getUser() {
			return User;
		}

	
	}




}
